package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;

import com.ajk.server.model.Friends;
import com.ajk.server.model.query.FriendsQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 亲友表  <br>
 * t_friends <br>
 * v1.0 2016-05-08
 * @see com.ajk.server.dao.FriendsMapper
 */
 public class FriendsProvider {
 
 	/**
 	* 新增 亲友表
 	*/
 	public String insert(Friends domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert ignore into t_friends(create_time,last_modify_time");
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getName()!=null){
			sb.append(",name");
		}
	 	if(domain.getAvatar()!=null){
			sb.append(",avatar");
		}
	 	if(domain.getMobile()!=null){
			sb.append(",mobile");
		}
	 	if(domain.getFriendsCategoryId()!=null){
			sb.append(",friends_category_id");
		}
	 	if(domain.getFriendsCategoryName()!=null){
			sb.append(",friends_category_name");
		}
	 	if(domain.getHeight()!=null){
			sb.append(",height");
		}
	 	if(domain.getWeight()!=null){
			sb.append(",weight");
		}
	 	if(domain.getBirthday()!=null){
	 		sb.append(",birthday");
	 	}
	 	if(domain.getSeq()!=null){
			sb.append(",seq");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getName()!=null){
			sb.append(",#{name}");
		}
	 	if(domain.getAvatar()!=null){
			sb.append(",#{avatar}");
		}
	 	if(domain.getMobile()!=null){
			sb.append(",#{mobile}");
		}
	 	if(domain.getFriendsCategoryId()!=null){
			sb.append(",#{friendsCategoryId}");
		}
	 	if(domain.getFriendsCategoryName()!=null){
			sb.append(",#{friendsCategoryName}");
		}
	 	if(domain.getHeight()!=null){
			sb.append(",#{height}");
		}
	 	if(domain.getWeight()!=null){
			sb.append(",#{weight}");
		}
	 	if(domain.getBirthday()!=null){
			sb.append(",#{birthday}");
		}
	 	if(domain.getSeq()!=null){
			sb.append(",#{seq}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 亲友表
 	*/
	public String update(Friends domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_friends set last_modify_time = now() ");
		
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getName()!=null){
			sb.append(",name=#{name}");
		}
		if(domain.getAvatar()!=null){
			sb.append(",avatar=#{avatar}");
		}
		if(domain.getMobile()!=null){
			sb.append(",mobile=#{mobile}");
		}
		if(domain.getFriendsCategoryId()!=null){
			sb.append(",friends_category_id=#{friendsCategoryId}");
		}
		if(domain.getFriendsCategoryName()!=null){
			sb.append(",friends_category_name=#{friendsCategoryName}");
		}
		if(domain.getHeight()!=null){
			sb.append(",height=#{height}");
		}
		if(domain.getWeight()!=null){
			sb.append(",weight=#{weight}");
		}
		if(domain.getBirthday()!=null){
			sb.append(",birthday=#{birthday}");
		}
		if(domain.getSeq()!=null){
			sb.append(",seq=#{seq}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<Friends>> map){
		StringBuffer sb = new StringBuffer();
		List<Friends> friendss = map.get("list");
		sb.append("insert into t_friends(create_time,last_modify_time,user_id,name,avatar,mobile,friends_category_id,friends_category_name,height,weight,birthday,seq,in_used) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].name}"
	    		+ ",#'{'list[{0}].avatar}"
	    		+ ",#'{'list[{0}].mobile}"
	    		+ ",#'{'list[{0}].friendsCategoryId}"
	    		+ ",#'{'list[{0}].friendsCategoryName}"
	    		+ ",#'{'list[{0}].height}"
	    		+ ",#'{'list[{0}].weight}"
	    		+ ",#'{'list[{0}].birthday}"
	    		+ ",#'{'list[{0}].seq}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(friendss)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  friendss.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 亲友表
 	*/
	public String findByQuery(FriendsQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_friends where in_used=1 ");
		
		if(query.getUserId()!=null){
			sb.append(" and user_id = #{userId}");
		}
		
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(FriendsQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_friends where in_used=1 ");
		
		if(query.getUserId()!=null){
			sb.append(" and user_id = #{userId}");
		}
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 亲友表
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_friends where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1=2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	